package com.coollang.smartbra.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.coollang.smartbra.R;
import com.coollang.smartbra.model.SportPlanModel;
import com.zhy.autolayout.utils.AutoUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

import static com.coollang.smartbra.utils.UIUtils.getResources;

/**
 * @author Timmy
 *         运动计划页面的日期
 */
public class SportPlanGridViewAdapter extends BaseAdapter {
    private Context mContext;
    private int currentPosition = -1;
    public List<SportPlanModel.DataBean> mDataList;
    private int mToDayPosition = -1;

    public SportPlanGridViewAdapter(Context context) {
        mContext = context;
    }

    @Override
    public int getCount() {
        if (mDataList != null) {
            return mDataList.size();

        }
        return 0;
    }

    @Override
    public Object getItem(int position) {
        return mDataList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    public int getCurrentPosition() {
        return currentPosition;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;

        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_sportplan_gridview, parent, false);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
            AutoUtils.autoSize(convertView);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        viewHolder.setData(position);
        return convertView;
    }


    public void addData(List<SportPlanModel.DataBean> dataList) {
        mDataList = dataList;
        notifyDataSetChanged();
    }

    public void notif(int position) {
        this.currentPosition = position;
        notifyDataSetChanged();
    }

    public void setToDayPosition(int todayPosition) {
        mToDayPosition = todayPosition;
    }

    class ViewHolder {
        private static final String TAG = "ViewHolder";
        @BindView(R.id.tv_top)
        TextView mTvTop;
        @BindView(R.id.tv_bottom)
        TextView mTvBottom;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }

        public void setData(int position) {
            SportPlanModel.DataBean dataBean = mDataList.get(position);
            String date = dataBean.getDate();

            // 获取今日的position
            // boolean today = TimeUtils.isToday(date);
            if (mToDayPosition == position) {
                mTvTop.setText("今日");
                mTvTop.setTextColor(getResources().getColor(R.color.progrss_bg));
                mTvBottom.setTextColor(getResources().getColor(R.color.progrss_bg));
            } else {
                mTvTop.setText(getWeekByDate(date));
                mTvTop.setTextColor(getResources().getColor(R.color.black_666_30));
                mTvBottom.setTextColor(getResources().getColor(R.color.black_666));
            }

            int day = Integer.parseInt(date.substring(date.length() - 2, date.length()));
            mTvBottom.setText(day + "");

            // 设备圆环背景
            if (currentPosition == position) {
                mTvBottom.setBackgroundResource(R.drawable.shape_yellow_pro);
                mTvBottom.setTextColor(getResources().getColor(R.color.white));
            } else {
                mTvBottom.setBackgroundResource(R.color.tra);
                if (mToDayPosition == position) {
                    mTvBottom.setTextColor(getResources().getColor(R.color.progrss_bg));
                } else {
                    mTvBottom.setTextColor(getResources().getColor(R.color.black_666));
                }
            }
        }

        // 判断当前是周几
        public String getWeekByDate(String date) {
            SimpleDateFormat fm = new SimpleDateFormat("yyyy-MM-dd");
            Date mdate = null;
            try {
                mdate = fm.parse(date);
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(mdate);
                return getDay(calendar.get(Calendar.DAY_OF_WEEK));
            } catch (ParseException e) {
                e.printStackTrace();
            }

            return "";
        }

        public String getDay(int date) {
            String week = "";
            switch (date) {
                case 1:
                    week = "周日";
                    break;
                case 2:
                    week = "周一";
                    break;
                case 3:
                    week = "周二";
                    break;
                case 4:
                    week = "周三";
                    break;
                case 5:
                    week = "周四";
                    break;
                case 6:
                    week = "周五";
                    break;
                case 7:
                    week = "周天";
                    break;
            }

            return week;
        }
    }


}